<template>
    <el-dialog :visible="visible_D" :center="true" width="460px" :before-close="handleClose">
        <span slot="title">分配角色</span>
        <el-form :model="ruleForm" :rules="rules" :label-position="labelPosition" ref="ruleForm" label-width="100px"
            class="demo-ruleForm">
            <el-form-item label="当前用户" prop="username">
                <el-input :disabled="true" v-model="ruleForm.username"></el-input>
            </el-form-item>
            <el-form-item label="分配新角色">
                <el-select v-model="ruleForm.role_name" @change="selectRoles" placeholder="请选择角色">
                    <el-option v-for="item in options" :key="item.value" :label="item.roleName" :value="item.id">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="submitForm('ruleForm')">保存</el-button>
                <el-button @click="handleClose">取消</el-button>
            </el-form-item>
        </el-form>
    </el-dialog>
</template>

<script>
import { getRoles, allotRoles } from '@/api/http';
export default {
    name: 'distribution',
    props: { visible_D: Boolean, userInfo: Object },
    data() {
        return {
            ruleForm: Object.assign({}, this.userInfo),
            labelPosition: 'right',
            rules: {},
            options: [],
            roles_id: ''
        }
    },
    watch: {
        visible_D: {
            immediate: true,
            handler(val) {
                val ? this.ruleForm = Object.assign(this.userInfo) : this.ruleForm = {}
            }
        }
    },
    mounted() {
        this.rolesList()
    },
    methods: {
        selectRoles(id) {
            this.roles_id = id
        },
        // 角色列表
        async rolesList() {
            const { data, meta } = await getRoles()
            if (meta.status == 200) {
                this.options = data
            }
        },
        // 分配用户角色
        async editRole() {
            try {
                const { data, meta } = await allotRoles(this.ruleForm.id, { rid: this.roles_id })
                if (meta.status == 200) {
                    this.$message.success(this.$t('message.updeteSuccess'))
                    this.updateVisible(false)
                    this.$emit('done')
                }
            } catch (error) {
                this.updateVisible(false)
                this.$emit('done')
            }
        },
        // 关闭弹窗
        handleClose() {
            this.updateVisible(false)
        },
        /* 更新visible */
        updateVisible(value) {
            this.$emit("update:visible_D", value);
        },
        // 提交
        submitForm(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    this.editRole()
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        },
    },
}
</script>

<style lang="scss" scoped>
</style>